package infostroy.bookagolf.components.widgets;

import com.academysmart.bookagolf.model.Order;
import infostroy.bookagolf.common.Constants.Colors;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 * Created by valer_000 on 2/18/14.
 */
public class GolfCalendarItem extends JPanel implements MouseListener {

    private JLabel timeLabel;
    //TODO:(Artem) change it to the type you need
    private List<Order> items;

    public GolfCalendarItem() {
        this.timeLabel = new JLabel();
        this.setBackground(Colors.CALENDAR_ITEM_NORMAL_STATE);
        this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.addMouseListener(this);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.initContent();
    }

    public GolfCalendarItem(String time) {
        this.timeLabel = new JLabel(time);
        this.setBackground(Colors.CALENDAR_ITEM_NORMAL_STATE);
        this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.addMouseListener(this);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.initContent();
    }

    public GolfCalendarItem(String time, List<Order> items) {
        this.timeLabel = new JLabel(time);
        this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.items = items;
        if (this.items != null && !this.items.isEmpty()) {
            this.setBackground(Colors.CALENDAR_ITEM_HAS_BOOKINGS);
        } else {
            this.setBackground(Colors.CALENDAR_ITEM_NORMAL_STATE);
        }
        this.addMouseListener(this);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.initContent();
    }

    private void initContent() {
        timeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(timeLabel);
        this.add(Box.createVerticalGlue());
        if (hasBookings()) {
            JLabel bookingsCount = new JLabel(Integer.toString(items.size()));
            bookingsCount.setForeground(Colors.CALENDAR_ITEM_BOOKINGS_COUNT);
            bookingsCount.setAlignmentX(Component.CENTER_ALIGNMENT);
            this.add(bookingsCount);
        }
    }

    public boolean hasBookings() {
        return items != null && !items.isEmpty();
    }

    public void setTimeLabel(String label) {
        if (timeLabel == null) {
            timeLabel = new JLabel(label);
        } else {
            timeLabel.setText(label);
        }
    }

    public List<Order> getItems() {
        return items;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        this.setBackground(Colors.CALENDAR_ITEM_HOVER_STATE);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (hasBookings()) {
            this.setBackground(Colors.CALENDAR_ITEM_HAS_BOOKINGS);
        } else {
            this.setBackground(Colors.CALENDAR_ITEM_NORMAL_STATE);
        }
    }
}
